/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.core;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.dltk.core.IBuildpathEntry;
import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.dltk.core.ModelException;
import org.eclipse.dltk.internal.core.OpenableElementInfo;
import org.eclipse.php.core.INamespaceFragmentRoot;

/**
 * The element info for <code>NamespaceFragmentRoot</code>s.
 */
@SuppressWarnings("restriction")
class NamespaceFragmentRootInfo extends OpenableElementInfo {

	/**
	 * The SourceMapper for this JAR (or <code>null</code> if this JAR does not
	 * have source attached).
	 */
	protected SourceMapper sourceMapper = null;

	/**
	 * The kind of the root associated with this info. Valid kinds are:
	 * <ul>
	 * <li><code>INamespaceFragmentRoot.K_SOURCE</code>
	 * <li><code>INamespaceFragmentRoot.K_BINARY</code>
	 * </ul>
	 */
	protected int rootKind = INamespaceFragmentRoot.K_SOURCE;

	/**
	 * A array with all the non-java resources contained by this
	 * NamespaceFragment
	 */
	protected Object[] nonJavaResources;

	/**
	 * Create and initialize a new instance of the receiver
	 */
	public NamespaceFragmentRootInfo() {
		this.nonJavaResources = null;
	}

	/**
	 * Starting at this folder, create non-java resources for this package
	 * fragment root and add them to the non-java resources collection.
	 *
	 * @exception ModelException The resource associated with this package
	 *                fragment does not exist
	 */
	static Object[] computeFolderNonPHPResources(INamespaceFragmentRoot root, IContainer folder,
			char[][] inclusionPatterns, char[][] exclusionPatterns) throws ModelException {
		// FIXME: Implement me!
		throw new RuntimeException(NamespaceFragmentRootInfo.class.getName()
				+ "#computeFolderNonPHPResources() not yet implemented!");
	}

	/**
	 * Compute the non-package resources of this package fragment root.
	 */
	private Object[] computeNonPHPResources(IResource underlyingResource, NamespaceFragmentRoot handle) {
		Object[] resources = NO_NON_SCRIPT_RESOURCES;
		try {
			// the underlying resource may be a folder or a project (in the case that the project folder
			// is actually the package fragment root)
			if (underlyingResource.getType() == IResource.FOLDER || underlyingResource.getType() == IResource.PROJECT) {
				resources = computeFolderNonPHPResources(handle, (IContainer) underlyingResource, handle
						.fullInclusionPatternChars(), handle.fullExclusionPatternChars());
			}
		}
		catch (ModelException e) {
			// ignore
		}
		return resources;
	}

	/**
	 * Returns an array of non-java resources contained in the receiver.
	 */
	synchronized Object[] getNonPHPResources(IScriptProject project, IResource underlyingResource,
			NamespaceFragmentRoot handle) {
		Object[] resources = this.nonJavaResources;
		if (resources == null) {
			resources = computeNonPHPResources(underlyingResource, handle);
			this.nonJavaResources = resources;
		}
		return resources;
	}

	/**
	 * Returns the kind of this root.
	 */
	public int getRootKind() {
		return this.rootKind;
	}

	/**
	 * Retuns the SourceMapper for this root, or <code>null</code> if this root
	 * does not have attached source.
	 */
	protected SourceMapper getSourceMapper() {
		return this.sourceMapper;
	}

	/**
	 * @param path
	 * @param resolvedBuildpath
	 * @return
	 */
	private static boolean isBuildpathEntry(IPath path, IBuildpathEntry[] resolvedBuildpath) {
		for (int i = 0, length = resolvedBuildpath.length; i < length; i++) {
			IBuildpathEntry entry = resolvedBuildpath[i];
			if (entry.getPath().equals(path)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Set the fNonPHPResources to res value
	 */
	void setNonPHPResources(Object[] resources) {
		this.nonJavaResources = resources;
	}

	/**
	 * Sets the kind of this root.
	 */
	protected void setRootKind(int newRootKind) {
		this.rootKind = newRootKind;
	}

	/**
	 * Sets the SourceMapper for this root.
	 */
	protected void setSourceMapper(SourceMapper mapper) {
		this.sourceMapper = mapper;
	}
}
